<template>
  <div class="studentList">
    <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
      <el-form-item label="姓名">
        <el-input v-model="formInline.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryStudentInfo">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="resetStudentInfo">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="compData" border style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" align="center">
      </el-table-column>
      <el-table-column prop="sex_text" label="性别" align="center">
      </el-table-column>
      <el-table-column prop="number" label="学号" align="center">
      </el-table-column>
      <el-table-column prop="class" label="班级" align="center">
      </el-table-column>
      <el-table-column prop="state_text" label="状态" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="电话" align="center">
      </el-table-column>
      <el-table-column prop="address" label="地址" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            class="el-icon-delete"
            @click="del(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 30, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getStudentList, delStudentInfo } from "@/api/index";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1, //当前页数
      pageSize: 10, //每页显示条数
      total: 0, //总条数
      formInline:{
        name:''
      }
    };
  },
  computed: {
    //分页： 1:1-10 2:11-20   slice((当前页数-1)*每页条数,当前页数*每页条数)
    compData() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    },
  },
  created() {
    this.getStudentsData();
  },
  methods: {
    //处理分页的两个方法
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1; //改变每页显示条数后，默认当前在第1页
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    //获取学生信息列表
    getStudentsData(params) {
      getStudentList(params)
        .then((res) => {
          // console.log(res);
          if (res.data.status === 200) {
            this.total = res.data.total;
            this.tableData = res.data.data;
            this.tableData.forEach((item) => {
              //尽量不要修改原数据
              item.sex === 1 ? (item.sex_text = "男") : (item.sex_text = "女");
              item.state === "1"
                ? (item.state_text = "已入学")
                : item.state === "2"
                ? (item.state_text = "未入学")
                : (item.state_text = "休学中");
            });
          }
        })
        .catch((error) => {
          throw error
        });
    },
    //删除学生信息方法
    del(row) {
      delStudentInfo(row.id).then((res) => {
        if (confirm("确定要删除吗？") && res.status === 200) {
          this.$message({ message: "删除数据成功", type: "success" });
          this.getStudentsData();
        }
      });
    },
    //学生信息查询方法
    queryStudentInfo(){
      this.getStudentsData(this.formInline)
    },

    //重置学生信息方法
    resetStudentInfo(){
      this.formInline={};
      this.getStudentsData(this.formInline)
    }
  }
};
</script>

<style lang="less" scoped>
.studentList {
  .el-pagination {
    text-align: left;
    margin-top: 20px;
  }
  .el-form{
    float: right;
    
  }
}
</style>